package com.example.taobaou.base;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.fragment.app.Fragment;

import com.example.taobaou.R;

public abstract class BaseFragment extends Fragment {

    private static final String TAG = "BaseFragment";
    private View rootView;
    private FrameLayout baseContainer;
    private State currenState = State.NONE;
    private View successView;
    private View loadingView;
    private View errorView;
    private View emptyView;
    ImageView re;
    private LinearLayout linearLayout;


    public enum State {

        NONE, LOADING, SUCCESS, ERROR, EMPTY

    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        rootView = loadRootView(inflater, container);
        baseContainer = rootView.findViewById(R.id.base_caontainer);
        loadStatesView(inflater, container);
        initView(rootView);
        initListener();
        initPresenter();
        loadData();
        return rootView;
    }

    /**
     * 设置相关监听事件的方法
     */
    protected void initListener() {

    }

    protected View loadRootView(LayoutInflater inflater, ViewGroup container) {
        return inflater.inflate(R.layout.base_fragment_layout, container, false);
    }


    public void retry(View view) {
        Log.d(TAG, "点击重试");
        //网络出问题时点击重试
        view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.d(TAG, "点击重试");
                onRetryClick();
            }
        });
    }

    protected void onRetryClick() {
    }

    /**
     * 加载各种状态下的view
     *
     * @param inflater
     * @param container
     */
    private void loadStatesView(LayoutInflater inflater, ViewGroup container) {
        //成功的页面
        successView = loadSuccessView(inflater, container);
        baseContainer.addView(successView);
        //加载中的页面
        loadingView = loadLoadingView(inflater, container);
        baseContainer.addView(loadingView);
        //错误的页面
        errorView = errorView(inflater, container);
        baseContainer.addView(errorView);
        //内容为空的额页面
        emptyView = emptyView(inflater, container);
        baseContainer.addView(emptyView);
        setState(State.NONE);
    }

    /**
     * 子类通过这个方法切换页面
     *
     * @param state
     */
    public void setState(State state) {
        this.currenState = state;
        successView.setVisibility(currenState == State.SUCCESS ? View.VISIBLE : View.GONE);
        loadingView.setVisibility(currenState == State.LOADING ? View.VISIBLE : View.GONE);
        errorView.setVisibility(currenState == State.ERROR ? View.VISIBLE : View.GONE);
        emptyView.setVisibility(currenState == State.EMPTY ? View.VISIBLE : View.GONE);

    }

    protected View emptyView(LayoutInflater inflater, ViewGroup container) {
        return inflater.inflate(R.layout.fragment_empty, container, false);
    }

    protected View errorView(LayoutInflater inflater, ViewGroup container) {
        View eview = inflater.inflate(R.layout.fragment_error, container, false);
        Log.d(TAG, "网络错误");
        View retry = eview.findViewById(R.id.retry);
        retry(retry);
        return eview;
    }

    protected View loadLoadingView(LayoutInflater inflater, ViewGroup container) {
        return inflater.inflate(R.layout.fragment_loading, container, false);
    }

    protected View loadSuccessView(LayoutInflater inflater, ViewGroup container) {

        int resId = getRootViewResID();
        return inflater.inflate(resId, container, false);
    }


    protected abstract int getRootViewResID();

    protected void initView(View rootView) {
        //绑定控件

    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        release();
    }


    protected void release() {
        //释放资源
    }

    protected void initPresenter() {
        //创建Presenter

    }

    protected void loadData() {
        //加载数据

    }

    //跳转页面
    protected void goTo(Class cls) {
        startActivity(new Intent(getActivity(), cls));
    }

}
